﻿using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;

namespace SBToolkit.Web
{
    /// <summary>
    /// Helper class for <see cref="WebControl"/>s.
    /// </summary>
    public static class WebControlHelper
    {
        /// <summary>
        /// Adds new item to the <see cref="ListControl"/> element.
        /// </summary>
        /// <remarks> 
        /// Applies to BulletedList, CheckBoxList, DropDownList, ListBox, RadioButtonList and other ListControl elements.</remarks>
        /// <param name="list"><see cref="ListControl"/> element.</param>
        /// <param name="text">Text to be added.</param>
        /// <param name="value">Value to be added.</param>
        public static void AddItem(this ListControl list, string text, string value)
        {
            list.Items.Add(new ListItem(text, value));
        }

        /// <summary>
        /// Gets the selected items.
        /// </summary>
        /// <remarks>Applies to BulletedList, CheckBoxList, DropDownList, ListBox, RadioButtonList and other ListControl elements.</remarks>
        /// <param name="list"><see cref="ListControl"/> element.</param>
        /// <returns>List of selected items.</returns>
        public static IList<ListItem> GetSelectedItems(this ListControl list)
        {
            return list.Items.Cast<ListItem>().Where(li => li.Selected).ToList();
        }
    }
}